package com.example.a12138.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.a12138.myapplication.Activity.PlayerActivity;
import com.example.a12138.myapplication.model.Realinfo;
import com.example.a12138.myapplication.presenter.RealinfoPresenter;
import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;
import com.xdw.mvp.pv.PresentView;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

public class PullLoadMoreActivity extends AppCompatActivity {
    private PullLoadMoreRecyclerView prv;
    private CommonAdapter<Realinfo> commonAdapter;
    private List<Realinfo> realinfoList=new ArrayList<>();
    private RealinfoPresenter realinfoPresenter;
    private int page=0;

    private PresentView<List<Realinfo>> presentView=new PresentView<List<Realinfo>>() {
        @Override
        public void onSuccess(List<Realinfo> realinfos) {
            realinfoList.addAll(realinfos);
            commonAdapter.notifyDataSetChanged();
            prv.setPullLoadMoreCompleted();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pull_load_more);
        prv=findViewById(R.id.prv);
        initPresenter();
        prv.setLinearLayout();
        /*realinfoList.add(new Realinfo("标题1","内容1",R.mipmap.ic_launcher));
        realinfoList.add(new Realinfo("标题2","内容2",R.mipmap.ic_launcher));
        realinfoList.add(new Realinfo("标题3","内容3",R.mipmap.ic_launcher));
        realinfoList.add(new Realinfo("标题4","内容4",R.mipmap.ic_launcher));
        realinfoList.add(new Realinfo("标题5","内容5",R.mipmap.ic_launcher));*/
        /*realinfoList.add(new Realinfo("标题1","内容1","https://th.wallhaven.cc/small/96/968d2x.jpg"));
        realinfoList.add(new Realinfo("标题2","内容2","https://th.wallhaven.cc/lg/dg/dge9ll.jpg"));
        realinfoList.add(new Realinfo("标题3","内容3","https://th.wallhaven.cc/small/dg/dgkzmj.jpg"));
        realinfoList.add(new Realinfo("标题4","内容4","https://th.wallhaven.cc/lg/6k/6kwle6.jpg"));
        realinfoList.add(new Realinfo("标题5","内容5","https://th.wallhaven.cc/small/13/13p6o1.jpg"));*/
        commonAdapter=new CommonAdapter<Realinfo>(this,R.layout.item_rv_realinfo,realinfoList) {
            @Override
            protected void convert(ViewHolder holder, Realinfo realinfo, int position) {
                Log.e("xxxx","title="+realinfo.getTitle());
                TextView tvTitle=holder.getView(R.id.tvTitle);
                tvTitle.setText(realinfo.getTitle());
                TextView tvContent=holder.getView(R.id.tvContent);
                tvContent.setText(realinfo.getContent());
               ImageView ivIcon=holder.getView(R.id.ivIcon);
               //ivIcon.setImageResource(realinfo.getIcon());//
                Glide.with(PullLoadMoreActivity.this).load(realinfo.getIcon_url()).into(ivIcon);

            }
        };



        prv.setAdapter(commonAdapter);

        commonAdapter.notifyDataSetChanged();
        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {

                Intent intent=new Intent(PullLoadMoreActivity.this,PlayerActivity.class);

                startActivity(intent);

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

        realinfoPresenter.getRealinfoList(0,0);
        prv.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {
            @Override
            public void onRefresh() {
                realinfoList.clear();
                page=0;
                realinfoPresenter.getRealinfoList(0,0);

            }

            @Override
            public void onLoadMore() {
                page++;
                realinfoPresenter.getRealinfoList(0,page);

            }
        });
    }

    private void initPresenter(){
        realinfoPresenter=new RealinfoPresenter(this);
        realinfoPresenter.onCreate();
        realinfoPresenter.BindPresentView(presentView);
    }

}
